<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of search
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Search extends MY_Controller {

    //put your code here


    function search() {
        parent :: __construct();
        $this->load->model('pickup_model');
        $this->load->model('location_model');
        $this->load->model('management_model');
        $this->load->model('listing_model');
        $this->load->model('requirement_model');
        $this->load->model('specification_model');
    }

//--Searching  ---------//    
    function go($type='CAR', $mode='USED') {
        if ($type == 'CAR' && $mode == 'USED') {
            $this->template->title('Welcome', 'carz.com')
                    ->set_partial('header', 'header/header')
                    ->set_partial('dealer', 'dealer/dealer')
                    ->set_partial('recent', 'recent/post')
                    ->set_partial('find_old', 'boxes/search_used')
                    ->set_partial('featured', 'boxes/featured')
                    ->set_partial('customer_care', 'sidebar/customer_care')
                    ->set_partial('social_share', 'sidebar/social_share')
                    ->set_partial('search_reviews', 'sidebar/search_reviews')
                    ->set_partial('recent_news', 'sidebar/recent_news')
                    ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                    ->set_partial('footer', 'footer/footer')
                    ->build('pages/index');
        }
        if ($type == 'CAR' && $mode == 'NEW') {
            $this->template->title('Welcome', 'carz.com')
                    ->set_partial('header', 'header/header')
                    ->set_partial('dealer', 'dealer/dealer')
                    ->set_partial('recent', 'recent/post')
                    ->set_partial('find_old', 'boxes/search_new')
                    ->set_partial('featured', 'boxes/featured')
                    ->set_partial('customer_care', 'sidebar/customer_care')
                    ->set_partial('social_share', 'sidebar/social_share')
                    ->set_partial('search_reviews', 'sidebar/search_reviews')
                    ->set_partial('recent_news', 'sidebar/recent_news')
                    ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                    ->set_partial('footer', 'footer/footer')
                    ->build('pages/index');
        }
    }

//--Searching Used or Old Car -------//    
    function used_car() {
        $post_query = array(
            'make' => $this->input->post('make'),
            'model' => $this->input->post('car_model'),
            'state' => $this->input->post('state'),
            'city' => $this->input->post('city'),
            'min_year' => $this->input->post('min_year'),
            'max_year' => $this->input->post('max_year'),
            'budget' => $this->input->post('budget'),
            'driven' => $this->pickup_model->get_pickup_info(13, $this->input->post('driven'))->f1,
        );

        $data['listing'] = $this->_search_used($post_query, 'CAR');
        $data['mode'] = 'USED_CAR';
        $this->template->set_layout('template_2');
        $this->template->title('Welcome', 'carz.com')
                ->set_partial('header', 'header/header')
                ->set_partial('recent', 'recent/post')
                ->set_partial('customer_care', 'sidebar/customer_care')
                ->set_partial('customer_care', 'sidebar/social_share')
                ->set_partial('social_share', 'sidebar/social_share')
                ->set_partial('search_reviews', 'sidebar/search_reviews')
                ->set_partial('recent_news', 'sidebar/recent_news')
                ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                ->set_partial('footer', 'footer/footer')
                ->build('pages/search_result', $data);
    }

//--Function Search (Subha-09/09/2012)----//
    private function _search_used($post, $type='CAR') {

        $sql = "Select * from listing_master where type='$type' and status = 51 ";
        if ($post['make']) {
            $sql.= " and make = " . $post['make'];
        }
        if ($post['model']) {
            $sql.= " and model = " . $post['model'];
        }
        if ($post['state']) {
            $sql.= " and state = " . $post['state'];
        }
        if ($post['city']) {
            $sql.= " and city = " . $post['city'];
        }
        if ($post['min_year']) {
            $sql.= " and make_year >= " . $post['min_year'];
        }
        if ($post['max_year']) {
            $sql.= " and make_year <= " . $post['max_year'];
        }
        if ($post['budget']) {
            $sql.= " and price <= " . $post['budget'];
        }
        if ($post['driven']) {
            $sql.= " and km_done <= " . $post['driven'];
        }
        //print $sql;

        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

//--Searching New Cars -----//
    function new_car() {
        $post_query = array(
            'make' => $this->input->post('car_make'),
            'body' => $this->input->post('car_body_type'),
            'fuel' => $this->input->post('car_fuel_type'),
            'transmission' => $this->input->post('car_transmission'),
            'budget' => $this->input->post('car_budget'),
        );
        
        //print_r($post_query);
        //die();
        

        $data['listing'] = $this->_search_new($post_query, 'CAR');
        $data['mode'] = 'NEW_CAR';
        $this->template->set_layout('template_2');
        $this->template->title('Welcome', 'carz.com')
                ->set_partial('header', 'header/header')
                ->set_partial('recent', 'recent/post')
                ->set_partial('customer_care', 'sidebar/customer_care')
                ->set_partial('customer_care', 'sidebar/social_share')
                ->set_partial('social_share', 'sidebar/social_share')
                ->set_partial('search_reviews', 'sidebar/search_reviews')
                ->set_partial('recent_news', 'sidebar/recent_news')
                ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                ->set_partial('footer', 'footer/footer')
                ->build('pages/search_result', $data);
    }

//--Function Search (Subha-09/09/2012)----//
    private function _search_new($post, $type='CAR') {

        $sql = "SELECT * from version 
                WHERE type='$type' 
                and status=51 
                ";
        //print_r($post);
        //die();
        
        if ($post['make']) {
            $sql.= " and make_id = " . $post['make'];
        }
        
        if ($post['body']) {
            $sql.= " and body = " . $post['body'];
        }
        
        if ($post['fuel']) {
            $sql.= " and fuel = " . $post['fuel'];
        }
        
        if ($post['transmission']) {
            $sql.= " and transmission = " . $post['transmission'];
        }
        
        if ($post['budget']) {
            $sql.= " and price <= " . $post['budget'];
        }
       
        $query = $this->db->query($sql);
        $result = $query->result();
        return $result;
    }

    
    
    
}

?>
